//
//  WNPreferenceVC.swift
//  WalkNovel
//
//  Created by Kevin on 2023/8/29.
//

import UIKit
import Magnetic

class WNPreferenceVC: WNBaseVC {
        
    lazy var preferenceView: PreferenceView = {
        let view = PreferenceView()
        view.saveBtn.rx.tap.bind { [weak self] in
            self?.savePreRequest()
        }.disposed(by: rx.disposeBag)
        return view
    }()
        
    var tagsArr = [TagsModel]() {
        didSet {
            handleManetic()
        }
    }
    
    var isAdd = false
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        if !isAdd {
            handleManetic()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewConfig() {
        view.addSubview(preferenceView)
        preferenceView.snp.makeConstraints { make in
            make.top.equalTo(NaviTotalHeight)
            make.left.right.equalTo(0)
            make.bottom.equalTo(-TabBarMargin)
        }
    }
    
    override func dataConfig() {
        pageName = "Preference"
        navView.titleName = "Preference"
        getPreRequest()
    }
}

extension WNPreferenceVC {
    
    func getPreRequest() {
        provider_user.rx.request(.getPreference)
            .asObservable().mapModel(BaseModel<[TagsModel]>.self)
            .subscribe(onNext: { [weak self] (model) in
                guard model.code == 0 else {
                    self?.wn_toast(model.msg ?? "")
                    return
                }
                if let data = model.data {
                    self?.tagsArr = data
                }
            }, onError: { [weak self] (error) in
                self?.wn_toast("Check the network connection")
            }).disposed(by: rx.disposeBag)
    }
    
    func handleManetic() {
        for item in tagsArr {
            let node = Node(text: item.label_name, color: .init(named: "gray_E0E8EE")!, radius: 40)
            node.scaleToFitContent = true
            node.padding = 16
            node.fontName = "PingFangSC-Semibold"
            node.fontSize = 13
            node.fontColor = .init(named: "gray_666666")!
            node.selectedFontColor = .white
            node.selectedColor = .init(named: "red_FE95B4")
            node.color = .init(named: "gray_E0E8EE")!
            if item.status == 2 {
                node.isSelected = true
                preferenceView.selArr.append(item.label_name ?? "")
            }
            preferenceView.styleView.magnetic.addChild(node)
        }
        isAdd = true
    }
    
    func savePreRequest() {
        guard preferenceView.selArr.count > 0 else {
            wn_toast("Choose topics no less than 1")
            return
        }
        var arr = [String]()
        for i in tagsArr {
            if (preferenceView.selArr.first(where: {$0 == i.label_name})?.count ?? 0) > 0 {
                arr.append(i.id ?? "")
            }
        }
        provider_user.rx.request(.savePreference(arr.joined(separator: ",")))
            .asObservable().mapModel(BaseModel<Any>.self)
            .subscribe(onNext: { [weak self] (model) in
                guard model.code == 0 else {
                    self?.wn_toast(model.msg ?? "")
                    return
                }
                self?.wn_toast(model.msg ?? "")
                self?.navigationController?.popViewController(animated: true)
            }, onError: { [weak self] (error) in
                self?.wn_toast("Check the network connection")
            }).disposed(by: rx.disposeBag)
    }
}

